package org.android.brasil.projetos.oktoberfest2011.view.controller;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Random;

import javax.xml.parsers.ParserConfigurationException;

import org.android.brasil.projetos.oktoberfest2011.R;
import org.android.brasil.projetos.oktoberfest2011.negocio.model.Apresentacao;
import org.android.brasil.projetos.oktoberfest2011.persistencia.parse.LerXML;
import org.android.brasil.projetos.oktoberfest2011.utils.AnalyticsUtils;
import org.android.brasil.projetos.oktoberfest2011.view.AgendaTabView;
import org.android.brasil.projetos.oktoberfest2011.view.CameraTabView;
import org.android.brasil.projetos.oktoberfest2011.view.HorariosTabView;
import org.android.brasil.projetos.oktoberfest2011.view.MapaTabView;
import org.android.brasil.projetos.oktoberfest2011.view.MeusEventosView;
import org.xml.sax.SAXException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Classe Main da aplicacao
 * 
 * @author douglas
 * 
 */
public class HomeController extends Activity implements OnClickListener {

	private static final int ABOUT = Menu.FIRST;

	private ImageView btProgramacao;
	//private ImageView btCamera;
	private ImageView btEvento;
	private ImageView btComida;
	private ImageView btHorario;
	private ImageView btMapa;
	private ImageView btTwitter;
	private TextView tvDesfile;
	private TextView tvAPartirDas;
	

	private AnalyticsUtils mGa;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.mGa = AnalyticsUtils.getInstance(this);

		setContentView(R.layout.home_view_new);

		this.btProgramacao = (ImageView) findViewById(R.idHomeView.btProgramacao);
		this.btEvento = (ImageView) findViewById(R.idHomeView.btComidaBebida);
//		this.btCamera = (ImageView) findViewById(R.idHomeView.btcamera);
		this.btComida = (ImageView) findViewById(R.idHomeView.btHorario);
		this.btHorario = (ImageView) findViewById(R.idHomeView.btMapa);
		this.btMapa = (ImageView) findViewById(R.idHomeView.btTwitter);
		this.btTwitter = (ImageView) findViewById(R.idHomeView.btEvento);
		this.tvDesfile = (TextView) findViewById(R.idHomeView.tv_desfile);
		this.tvAPartirDas = (TextView) findViewById(R.idHomeView.tv_a_partir);
		LerXML teste = new LerXML();
		try {
			
			teste.processarXml();
			
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//home screen
		this.mGa.trackScreen("home");
		
		inicializarObjetos();
	}

	@Override
	public void onResume() {
		super.onResume();
		inicializarObjetos();
	}

	/**
	 * Inicializa objetos da view home.
	 * 
	 */
	private void inicializarObjetos() {
		Random rdAle = new Random();
		String desfile = "";
		String aPartirDas = "";

		this.btProgramacao.setOnClickListener(this);
		//this.btCamera.setOnClickListener(this);
		this.btEvento.setOnClickListener(this);
		this.btComida.setOnClickListener(this);
		this.btHorario.setOnClickListener(this);
		this.btMapa.setOnClickListener(this);
		this.btTwitter.setOnClickListener(this);

		String[] frases1 = getResources().getStringArray(R.array.frases1);
		String[] frases2 = getResources().getStringArray(R.array.frases2);
		// String desfile = getResources().getString(R.string.nao_tem_desfile);
		// String aPartirDas = getResources().getString(R.string.tomar_umas);

		int maxI = (frases1.length > frases2.length) ? frases2.length : frases1.length;
		int intRand = rdAle.nextInt(maxI + 1);

		if (intRand == maxI) {
			int diasEvento = WidgetController.getDiasDeEvento();
			if (diasEvento > 0 && diasEvento <= 17) {
				desfile = diasEvento + " "
						+ getResources().getString(R.string.dias_bebedeira);
				aPartirDas = getResources().getString(R.string.cotinue_assim);
			} else {
				intRand--;
			}
		}

		if (desfile.equals("") || aPartirDas.equals("")) {
			desfile = frases1[intRand];
			aPartirDas = frases2[intRand];
		}

		if (WidgetController.temDesfileHoje(getApplicationContext())) {
			desfile = getResources().getString(R.string.tem_desfile);
			Apresentacao apr = WidgetController.getDesfileDeHoje(getApplicationContext());
			String inicio = new DecimalFormat("00.00").format(apr.getHoraInicio())
					.replaceAll(",", "h").replaceAll("\\.", "h");
			aPartirDas = getResources().getString(R.string.a_partir) + " " + inicio + " "
					+ apr.getSetor();
			tvDesfile.setOnClickListener(new OnClickListener() {

				public void onClick(View arg0) {
					Intent i = new Intent(getApplicationContext(), HorariosTabView.class);
					startActivity(i);

				}
			});

			tvAPartirDas.setOnClickListener(new OnClickListener() {

				public void onClick(View arg0) {
					Intent i = new Intent(getApplicationContext(), HorariosTabView.class);
					startActivity(i);

				}
			});
		}

		tvDesfile.setText(desfile);
		tvAPartirDas.setText(aPartirDas);

	}

	public void onClick(View v) {
		Intent i = null;

		switch (v.getId()) {
/*		case R.idHomeView.btcamera:
			this.mGa.buttonTrackEvent("Camera");
			this.startActivity(new Intent(this, CameraTabView.class));
			break;
*/
		case R.idHomeView.btProgramacao:
			this.mGa.buttonTrackEvent(getString(R.string.agenda));
			this.startActivity(new Intent(this, AgendaTabView.class));
			break;

		case R.idHomeView.btEvento:
			this.mGa.buttonTrackEvent(getString(R.string.meusEventos));
			i = new Intent(this, MeusEventosView.class);
			this.startActivity(i);
			break;

		case R.idHomeView.btComidaBebida:
			this.mGa.buttonTrackEvent(getString(R.string.comidaBebida));
/*			Toast.makeText(getApplicationContext(), "Em breve...", Toast.LENGTH_LONG)
					.show();*/
			 this.startActivity(new Intent(this, BebidaComidaController.class));
			break;

		case R.idHomeView.btHorario:
			this.mGa.buttonTrackEvent(getString(R.string.horario));
			i = new Intent(this, HorariosTabView.class);
			this.startActivity(i);
			break;

		case R.idHomeView.btMapa:
			this.mGa.buttonTrackEvent(getString(R.string.mapa));
			this.startActivity(new Intent(this, MapaTabView.class));
			break;

		case R.idHomeView.btTwitter:
			this.mGa.buttonTrackEvent(getString(R.string.oktoberfestTwitter));
			this.startActivity(new Intent(this, TwitterController.class));
			break;

		}

	}

	// -------Create the menu-------
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		menu.add(0, ABOUT, 0, R.string.btn_about).setIcon(R.drawable.ic_info);

		return super.onCreateOptionsMenu(menu);
	}

	// handle the menu buttons
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent i = null;

		switch (item.getItemId()) {

		case ABOUT:
			this.mGa.trackEvent("Click", "Menu", getString(R.string.btn_about));
			i = new Intent(this, SobreController.class);
			startActivity(i);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	protected void onDestroy() {
		super.onDestroy();
	}

}
